function out = equilibrium_function_v2(v_sort,mc,q,theta,bw,smooth_type,plot_switch,plot_number,plot_dir,plot_name,plot_version)
%Calculate and plot equilibrium

%ms and mr
q_prime = diff(q);
v_prime = diff(v_sort);
dqdp = q_prime./v_prime;	
dqdp = [dqdp; dqdp(end)];	

ms = q./dqdp;	%marginal consumer surplus: see Section 2 Model			
ms = smooth(ms,bw,smooth_type);

mr = v_sort + ms;		

%mc and ac
ac = zeros(size(mc));
for i = 1:length(mc)
    ac(i) = mean(mc(1:i,1));	
end

%adjust for market power
mc_comp = theta*mc + (1-theta)*ac;	
mr_comp = v_sort + theta*ms;		

%% First best 		
[value_fb index_fb] = min(abs(v_sort-mc)); %#ok<ASGLU>
q_fb = q(index_fb);	
p_fb = v_sort(index_fb);	

%Display first best outputs
fprintf('\n\nFirst best \n')
fprintf('Price is %f \n', p_fb)
fprintf('Quantity is %f \n', q_fb)
fprintf('Average costs is %f \n', mean(mc(1:index_fb)));
fprintf('Marginal costs is %f \n', mc(index_fb));

costs_fb                = sum(mc(1:index_fb));	
producer_surplus_fb     = p_fb*index_fb - costs_fb;	
consumer_surplus_fb     = sum(v_sort(1:index_fb)) - p_fb*index_fb;	
total_surplus_fb        = producer_surplus_fb + consumer_surplus_fb;	

fprintf('Total costs is %f \n',         costs_fb);
fprintf('Total revenue is %f \n',       p_fb*index_fb);
fprintf('Producer surplus is %f \n',    producer_surplus_fb);
fprintf('Consumers surlus is %f \n',    consumer_surplus_fb);
fprintf('Total surplus is %f \n\n',     total_surplus_fb);

out_fb = [p_fb; q_fb; costs_fb; p_fb*index_fb; producer_surplus_fb; consumer_surplus_fb; total_surplus_fb];

%% Equilibrium
[value index] = min(abs(mr_comp - mc_comp)); %#ok<ASGLU> %Equation (1) in Section 2.1
q_star = q(index);	
p_star = v_sort(index);

mr = max(mr,-500);
mr_comp = max(mr_comp,-500);

%Display equilibrium outputs
fprintf('\n\nEquilibrium \n')
fprintf('Theta is %f \n', theta);
fprintf('Price is %f \n', p_star);
fprintf('Quantity is %f \n', q_star);
fprintf('Average costs is %f \n', mean(mc(1:index)));
fprintf('Marginal costs is %f \n', mc(index));

costs               = sum(mc(1:index));		
producer_surplus    = p_star*index - costs;
consumer_surplus    = sum(v_sort(1:index)) - p_star*index;
total_surplus       = producer_surplus + consumer_surplus;

fprintf('Total costs is %f \n',         costs);
fprintf('Total revenue is %f \n',       p_star*index);
fprintf('Producer surplus is %f \n',    producer_surplus);
fprintf('Consumers surlus is %f \n',    consumer_surplus);
fprintf('Total surplus is %f \n\n',     total_surplus);

out_star = [p_star; q_star; costs; p_star*index; producer_surplus; consumer_surplus; total_surplus];

out = [out_fb out_star];	

%% Plot equilibrium
if plot_switch == 1,	

    figure(plot_number)
    hold all;
    plot(q,v_sort,'LineStyle', '-','Color',[0.5, 0.5, 0.5], 'linewidth',2);
    plot(q,mr,'LineStyle', ':','Color',[0.5, 0.5, 0.5], 'linewidth',2);
    plot(q,mr_comp,'LineStyle', '--','Color',[0.5, 0.5, 0.5], 'linewidth',2);
    plot(q,ac,'LineStyle', '-','Color',[0, 0, 0], 'linewidth',2);
    plot(q,mc,'LineStyle', ':','Color',[0, 0, 0], 'linewidth',2);
    plot(q,mc_comp,'LineStyle', '--','Color',[0, 0, 0], 'linewidth',2);
    box on

    ylabel('Price and Cost'); xlabel('Quantity');
    axis([0 inf 0 inf]);
    legend('Demand','MR','Perceived MR','AC','MC','Perceived MC'); legend('Location','NorthEast'); legend('boxoff');
    str1(1)={['p* = ',num2str(round(p_star))]};
    str1(2)={['q* = ', num2str(round(q_star, 3))]};
    text(.4,.9*v_sort(1,1),str1,'FontSize',13)
    
    f_out = ['../Figures/' plot_dir 'Equilibrium_' plot_name '_' plot_version '.pdf'];
    hgsave(f_out)
    print('-painters','-dpdf','-r300',f_out);
    
end